﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TY.Webs.ClassLib;
using JCW.Common;
using Leefish.Market.PRO.BLL;
using Leefish.Market.PRO.Entity;
using System.Collections.Generic;

namespace Leefish.Market.Web.Handler
{
    /// <summary>
    ///AdminHandle 的摘要说明
    /// </summary>
    public class CommonHandler:CommonPageBase
    {
        private BzWinBLL bzBLL;
        public CommonHandler()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
            /*EnRoller roller = new EnRoller();
            roller.BeginTime = DateTime.Now.AddDays(-1);
            roller.EndTime = DateTime.Now.AddDays(10);
            roller.Notice = "这是一个奖品";
            roller.PID = 1;
            roller.PName = "抽奖大风轮";
            roller.Probability =Convert.ToDecimal(30.5);
            roller.ListAward = GetList();

            bzBLL = new BzWinBLL(roller);
            */
        }

        public override void ProcessRequest(HttpContext context){
            //base.ProcessRequest(context);
            //context.Response.Write("aaaaaaaaaa");
            Transfers tf = new Transfers();
            switch (action) {
                case "loginCheck": tf = CheckValidCode(context); break;     //验证用户登录

                case "trigger": tf = Trigger(context); break;//抽奖


            }
            context.Response.Write(tf.ToJson());
            context.Response.End();
        }

        private Transfers Trigger(HttpContext context)
        {
            Transfers tf = new Transfers();
            int rel = 0;
            string nick = requestUtility.GetFormString("nick");
            if (!string.IsNullOrEmpty(nick))
            {
                BzWinBLL bzBLL = BzMainBL.Get(nick);
                if (bzBLL != null)
                {
                    rel = bzBLL.Surprise();
                }
                else
                {
                    rel = -3;//找不到
                }
            }
            else {
                rel = -3;
            }
            tf.AddColumn("rel", rel);
            return tf;
        }

        private List<EnAward> GetList()
        {
            List<EnAward> list = new List<EnAward>();
            EnAward a1 = new EnAward();
            a1.AID = 1;
            a1.Probability = 10;
            a1.AName = "奖品1";
            list.Add(a1);

            EnAward a2 = new EnAward();
            a2.AID = 2;
            a2.Probability = 50;
            a2.AName = "奖品2";
            list.Add(a2);

            EnAward a3 = new EnAward();
            a3.AID = 3;
            a3.Probability = 10;
            a3.AName = "奖品3";
            list.Add(a3);

            EnAward a4 = new EnAward();
            a4.AID = 4;
            a4.Probability = 10;
            a4.AName = "奖品4";
            list.Add(a4);

            EnAward a5 = new EnAward();
            a5.AID = 5;
            a5.Probability = 9.5m;
            a5.AName = "奖品5";
            list.Add(a5);

            EnAward a6 = new EnAward();
            a6.AID = 6;
            a6.Probability = 5;
            a6.AName = "奖品6";
            list.Add(a6);

            EnAward a7 = new EnAward();
            a7.AID = 7;
            a7.Probability = 5;
            a7.AName = "奖品7";
            list.Add(a7);

            EnAward a8 = new EnAward();
            a8.AID = 8;
            a8.Probability = 0.5m;
            a8.AName = "奖品8";
            list.Add(a8);

            return list;


        }
        private void TriggerAword()
        {

            

            ///taobao.vas.subscribe.get 订购关系查询  http://api.taobao.com/apidoc/api.htm?spm=0.0.0.0.uDX03r&path=cid:10124-apiId:10586
            /*返回结果
             * {
                    "vas_subscribe_get_response": {
                        "article_user_subscribes": {
                            "article_user_subscribe": [{
                                "item_code": "ts-123-1",
                                "deadline": "2000-01-01 00:00:00"
                            }]
                        }
                    }
                }

            ITopClient client = new DefaultTopClient(url, appkey, appsecret);
            VasSubscribeGetRequest req = new VasSubscribeGetRequest();
            req.Nick = "hz0799";
            req.ArticleCode = "ts-1234";
            VasSubscribeGetResponse response = client.Execute(req);
            */

        }
        private Transfers CheckValidCode(HttpContext context)
        {
            Transfers tf = new Transfers();
            int result = 0;
            string msg = "";
            string code = requestUtility.GetFormString("code");
            try
            {
                if (context.Session["Identify"].ToString().ToLower() == code.ToString().ToLower())
                {
                    result = 1;
                }
                else
                {
                    msg = "wrong volidcode";
                }
            }
            catch (Exception ex)
            { 
                
            }
            tf.AddColumn("result", result);
            tf.AddColumn("msg", msg);
            return tf;
        }

    }
}